import 'dart:async';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_app/common/global.dart';
import 'package:flutter_app/model/tweet.dart';

import 'package:flutter_app/style/color.dart';
import 'package:flutter_app/tools/utils.dart';

// ignore: must_be_immutable
// ignore: camel_case_types
class menuWidget extends StatefulWidget {
  dynamic mode;
  double iconSize;
  menuWidget(this.mode, {Key key, this.iconSize = 24.0}) : super(key: key);

  @override
  _menuWidgetState createState() => _menuWidgetState();
}

// ignore: camel_case_types
class _menuWidgetState extends State<menuWidget> {
  int likedNum = 0;
  Timer t;

  refreshLiked(mode) async {
    t?.cancel();

    t = Timer(Duration(milliseconds: 200), () {
      var dio = new Dio();
      //动态
      if (mode.runtimeType.toString() == "Tweet") {
        if (likedNum > 0) {
          FormData formData =
              FormData.fromMap({"tweetId": mode.id, "userId": Global.userId});
          dio.post(Global.serversUrl + "/Liked", data: formData);
        } else if (likedNum == 0) {
          FormData formData =
              FormData.fromMap({"tweetId": mode.id, "userId": Global.userId});
          dio.post(Global.serversUrl + "/NoFeeling", data: formData);
        } else if (likedNum < 0) {
          FormData formData =
              FormData.fromMap({"tweetId": mode.id, "userId": Global.userId});
          dio.post(Global.serversUrl + "/Dissatisfied", data: formData);
        }
      }
      //回复
      else if (mode.runtimeType.toString() == "Reply") {
        if (likedNum > 0) {
          FormData formData =
              FormData.fromMap({"replyId": mode.id, "userId": Global.userId});
          dio.post(Global.serversUrl + "/ReplyLiked", data: formData);
        } else if (likedNum == 0) {
          FormData formData =
              FormData.fromMap({"replyId": mode.id, "userId": Global.userId});
          dio.post(Global.serversUrl + "/ReplyNoFeeling", data: formData);
        } else if (likedNum < 0) {
          FormData formData =
              FormData.fromMap({"replyId": mode.id, "userId": Global.userId});
          dio.post(Global.serversUrl + "/ReplyDissatisfied", data: formData);
        }
      }
    });
  }

  Widget buildCommentButton(mode) {
    if (mode.runtimeType.toString() == "Tweet") {
      return Row(
        children: <Widget>[
          IconButton(
            padding: EdgeInsets.fromLTRB(0, 0, 10, 0),
            alignment: Alignment.centerRight,
            icon: Icon(Icons.comment),
            color: m_color.grey,
            onPressed: null,
          ),
          SizedBox(
            width: 54,
            child: Container(
              child: Text(
                m_utils.numToString(mode.repliesCount),
                maxLines: 1,
              ),
            ),
          ),
        ],
      );
    } else
      return Container();
  }

  //点赞
  void _toggleFavorite(i) {
    //喜欢
    if (i > 0) {
      //之前点了不喜欢
      if (widget.mode.dissatisfied) {
        widget.mode.dissatisfiedCount -= 1;
        widget.mode.dissatisfied = false;
      }
      if (widget.mode.isFavorited) {
        widget.mode.favoriteCount -= 1;
        widget.mode.isFavorited = false;
        likedNum = 0;
      } else {
        likedNum = 1;
        widget.mode.favoriteCount += 1;
        widget.mode.isFavorited = true;
      }
    }
    //不喜欢
    else {
      if (widget.mode.isFavorited) {
        widget.mode.favoriteCount -= 1;
        widget.mode.isFavorited = false;
      }
      if (widget.mode.dissatisfied) {
        widget.mode.dissatisfiedCount -= 1;
        widget.mode.dissatisfied = false;
        likedNum = 0;
      } else {
        widget.mode.dissatisfiedCount += 1;
        widget.mode.dissatisfied = true;
        likedNum = -1;
      }
    }
    refreshLiked(widget.mode);
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        padding: EdgeInsets.all(0),
        alignment: Alignment.center,
        child: Row(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            //点赞
            Container(
              margin: EdgeInsets.fromLTRB(10, 0, 0, 0),
              child: InkWell(
                child: Container(
                    padding: EdgeInsets.fromLTRB(7, 10, 7, 10),
                    child: Row(
                      children: [
                        SizedBox(
                          child: Icon(
                            (widget.mode.isFavorited
                                ? Icons.favorite
                                : Icons.favorite_border),
                            color: (widget.mode.isFavorited
                                ? m_color.red
                                : m_color.grey),
                            size: widget.iconSize,
                          ),
                        ),
                        SizedBox(
                          width: 54,
                          child: Container(
                            child: Text(
                              m_utils.numToString(widget.mode.favoriteCount),
                              maxLines: 1,
                            ),
                          ),
                        ),
                      ],
                    )),
                onTap: () {
                  _toggleFavorite(1);
                },
              ),
            ),

//不喜欢
            Container(
              child: InkWell(
                child: Container(
                    padding: EdgeInsets.fromLTRB(7, 10, 7, 10),
                    child: Row(
                      children: [
                        SizedBox(
                          child: Icon(
                            Icons.sentiment_dissatisfied,
                            color: (widget.mode.dissatisfied
                                ? m_color.dissatisfied
                                : m_color.grey),
                            size: widget.iconSize,
                          ),
                        ),
                        SizedBox(
                          width: 54,
                          child: Container(
                            child: Text(
                              m_utils
                                  .numToString(widget.mode.dissatisfiedCount),
                              maxLines: 1,
                            ),
                          ),
                        ),
                      ],
                    )),
                onTap: () {
                  _toggleFavorite(-1);
                },
              ),
            ),

            //评论
            buildCommentButton(widget.mode)
          ],
        ));
  }

  @override
  void dispose() {
    super.dispose();
    t?.cancel();
  }
}
